﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.IoC;
using Kooboo.Domain;

namespace Kooboo.Commerce
{
    public static class ProductTypeCommand
    {
        public static void Enable(this IProductType productType)
        {
            if (productType != null)
            {
                var repository = ContextContainer.Current.Resolve<IProductTypeRepository>();

                using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
                {
                    try
                    {
                        repository.ProductTypes
                            .Where(i => i.Id == productType.Id)
                            .ToUpdate()
                            .Set(i => i.IsEnable, true)
                            .Execute();
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }

        public static void Disable(this IProductType productType)
        {
            if (productType != null)
            {
                var repository = ContextContainer.Current.Resolve<IProductTypeRepository>();

                using (var transaction = ObjectContainer.CreateInstance<TransactionContext>())
                {
                    try
                    {
                        repository.ProductTypes
                            .Where(i => i.Id == productType.Id)
                            .ToUpdate()
                            .Set(i => i.IsEnable, false)
                            .Execute();
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
    }
}
